﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    public class CityController
    {

        /// <summary>
        /// 省 数据库访问
        /// </summary>
        private hf.DAL.Province provinceDAL = new hf.DAL.Province();

        /// <summary>
        /// 省份列表
        /// </summary>
        private List<hf.Model.Province> provinceList = null;

        /// <summary>
        /// IP库数据库访问
        /// </summary>
        private hf.DAL.IPAddress ipaddressDAL = new hf.DAL.IPAddress();

        /// <summary>
        /// 英文字母
        /// </summary>
        private hf.DAL.EnglishKeyDAL englishKeyDAL = new hf.DAL.EnglishKeyDAL();

        /// <summary>
        /// 城市列表
        /// </summary>
        private List<hf.Model.City> cityList = null;

        private List<hf.Model.EnglishKeyModel> englishKeyList = null;

        /// <summary>
        /// 得到静态的英文字母列表
        /// </summary>
        /// <returns></returns>
        public List<hf.Model.EnglishKeyModel> getEnglishKeyList()
        {
            if (this.englishKeyList == null)
            {
                int total;
                this.englishKeyList = englishKeyDAL.GetKeyList(26, 1, "", out total);
            }
            return this.englishKeyList;
        }

        /// <summary>
        /// 根据字母得到列表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<hf.Model.Province> getProvinceByEngKey(string key)
        {

            return this.getProvinceList().FindAll(i => i.EngKey.Trim().ToLower() == key.Trim().ToLower());
        }
        /// <summary>
        /// 得到静态省份列表
        /// </summary>
        /// <returns></returns>
        public List<hf.Model.Province> getProvinceList() {
            if (this.provinceList == null)
            {
                provinceList = this.getProvinceList(9999);
            }
            foreach (hf.Model.Province p in provinceList)
            {
                p.ProvinceName = p.ProvinceName.Replace("市", "").Replace("省", "").Replace("特别行政区", "").Replace("自治区", "").Replace("壮族", "").Replace("回族", "").Replace("维吾尔","").Replace("藏族","");
            }
            return provinceList;
        }

        /// <summary>
        /// 得到省会城市
        /// </summary>
        /// <returns></returns>
        public List<hf.Model.City> getProvinceCityList()
        {
                List<hf.Model.City> list = new List<hf.Model.City>();
                //记录总条数
                int total;
                //筛选条件
                string strSql = "";
            string tempStrSql = "";
                foreach (hf.Model.Province p in this.getProvinceList())
                {
                    if (tempStrSql == "")
                    {
                        tempStrSql = p.ID.ToString();
                    }
                    else {
                        tempStrSql =tempStrSql + "," + p.ID.ToString();
                    }
                }

                strSql = "ProvinceID in (" + tempStrSql + ")";

                //获得列表
                cityList = this.cityDAL.GetCityList(9999, 1, strSql, out total);

                string[] Array = tempStrSql.Split(',');
                foreach (string s in Array)
                {
                    hf.Model.City c = cityList.Find(i => i.ProvinceID == int.Parse(s));
                    list.Add(c);
                }

                foreach (hf.Model.City c in list)
                {
                    c.CityName = c.CityName.Replace("市", "");
                    c.CityName = c.CityName.Replace("县", "");
                    c.CityName = c.CityName.Replace("特别行政区", "");
                }

                return list;
        }

        /// <summary>
        /// 得到静态城市列表
        /// </summary>
        /// <returns></returns>
        public List<hf.Model.City> getCityList()
        {
            if (this.cityList == null)
            {
                List<hf.Model.City> list = new List<hf.Model.City>();
                //记录总条数
                int total;
                //筛选条件
                string strSql = "";
                //获得列表
                cityList = this.cityDAL.GetCityList(99999, 1, strSql, out total);

                foreach (hf.Model.City c in cityList)
                {
                    c.CityName = c.CityName.Replace("市", "");
                    c.CityName = c.CityName.Replace("县", "");
                }
            }
            return cityList;
        }

        /// <summary>
        /// 市 数据库访问
        /// </summary>
        private hf.DAL.City cityDAL = new hf.DAL.City();

        /// <summary>
        /// 单列
        /// </summary>
        private static CityController _cityController;

        /// <summary>
        /// 实例化单列
        /// </summary>
        /// <returns></returns>
        public static CityController getController()
        {
            if (_cityController == null)
            {
                _cityController = new CityController();
            }
            return _cityController;
        }



        /// <summary>
        /// 得到IP地址
        /// </summary>
        /// <param name="IPAddress"></param>
        /// <returns></returns>
        public hf.Model.IPAddress ipAddress(string IPAddress)
        {
            string[] IPAddressArray = IPAddress.Split('.');

            double ip = double.Parse(IPAddressArray[0]) * 256 * 256 * 256 + double.Parse(IPAddressArray[1]) * 256 * 256 + double.Parse(IPAddressArray[2]) * 256 + double.Parse(IPAddressArray[3]);
            /*
            double startip = double.Parse(IPAddressArray[0]) * 256 * 256 * 256 + double.Parse(IPAddressArray[1]) * 256 * 256 + double.Parse(IPAddressArray[2]) * 256 + 0;
            double endip = double.Parse(IPAddressArray[0]) * 256 * 256 * 256 + double.Parse(IPAddressArray[1]) * 256 * 256 + double.Parse(IPAddressArray[2]) * 256 + 255;
            */
            string sqlWhere = ip.ToString() + " between IP1 and IP2"; //"IP1 = " + startip.ToString() + " and IP2 = " + endip.ToString();// ip.ToString() + " between IP1 and IP2";

            int total = 0;
            List<hf.Model.IPAddress> list = ipaddressDAL.GetIPAddressList(1, 1, sqlWhere, out total);

            //HttpContext.Current.Response.Write(sqlWhere + "<br />");
            if (list.Count > 0)
                return list[0];
            else
                return null;
            /*
            Startip: 1*256*256*256+1*256*256+0*256+0 = 16842752 (即字段IP1)

                    Endip: 1*256*256*256+1*256*256+0*256+255 = 16843007 (即字段IP2)
             * */
        }

        /// <summary>
        /// 得到省份列表
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.Province> getProvinceList(int Count)
        {
            List<hf.Model.Province> list = new List<hf.Model.Province>();

            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = this.provinceDAL.GetProvinceList(Count, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到所属城市列表
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="provinceID"></param>
        /// <returns></returns>
        public List<hf.Model.City> getCityList(int Count, string provinceID)
        {
            List<hf.Model.City> list = new List<hf.Model.City>();

            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //筛选条件
            string strSql = "ProvinceID = " + provinceID;

            //获得列表
            list = this.cityDAL.GetCityList(Count, PageNumber, strSql, out total);

            return list;
        }
    }
}
